package com.chaodev.base.controller;

import com.chaodev.base.base.RedisDao;
import com.chaodev.base.model.ActivityClock;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.ResultEnum;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.ActivityClockService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("articleClock")
@Api(tags = "活动打卡")
public class ActivityClockController {

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private ActivityClockService activityClockService;

    @PostMapping(value = "/addActivityClock")
    @ApiOperation(value = "新增活动打卡")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result addActivityClock(@RequestBody ActivityClock activityClock, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (activityClock.getActivityId() == null || "".equals(activityClock.getActivityId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少活动id");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            activityClock.setClockUserCode(userInfo.getUserId());
            activityClock.setClockUserName(userInfo.getUserName());
            if (activityClockService.addActivityClock(activityClock)){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(activityClock.toString(), "新增活动打卡", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getUserActivityClockList")
    @ApiOperation(value = "查询用户打卡信息列表")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getUserActivityClockList(@RequestBody ActivityClock activityClock, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }

            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            activityClock.setClockUserCode(userInfo.getUserId());

            result.setData(activityClockService.getActivityClockList(activityClock));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("操作成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(activityClock.toString(), "查询用户打卡信息列表", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }
}
